package com.gpjcjl.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.base.lib.SharedHelper;
import com.gpjcjl.BaseActivity;
import com.gpjcjl.Config;
import com.gpjcjl.R;
import com.gpjcjl.entity.SettingProfile;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 制造年月
 */
public class SettingItem3 extends BaseActivity implements OnItemClickListener {

    @BindView(R.id.listView)
    SwipeRecyclerView listView;
    private CommonAdapter<String> adapter;


    @Override
    protected int getContentView() {
        return R.layout.fragment_item3;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView.setOnItemClickListener(this);

        List<String> list = new ArrayList<>();
        list.add("录入");
        list.add("自动");
        listView.setAdapter(adapter = new CommonAdapter<String>(mContext, R.layout.item_create_time, list) {
            @Override
            protected void convert(ViewHolder holder, String o, int position) {
                holder.setText(R.id.tv, o);
                String produceTime = SettingProfile.getProduceTimeType();
                holder.setVisible(R.id.iv, o.equals(produceTime));
                holder.setVisible(R.id.btnView, "自动".equals(o) && "自动".equals(produceTime));
                String startTime = SettingProfile.getProduceTimeStart();
                String endTime = SettingProfile.getProduceTimeEnd();
                if (!TextUtils.isEmpty(startTime)) {
                    holder.setText(R.id.tv_start_time, startTime);
                }
                if (!TextUtils.isEmpty(endTime)) {
                    holder.setText(R.id.tv_end_time, endTime);
                }
                showDatePicker(holder.getView(R.id.tv_start_time), Config.DEFAULT_PRODUCE_DATE_START);
                showDatePicker(holder.getView(R.id.tv_end_time), Config.DEFAULT_PRODUCE_DATE_END);
            }
        });
    }

    private void showDatePicker(TextView tv, String key) {
        tv.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            View view = getLayoutInflater().inflate(R.layout.dialog_datepicker, null);
            DatePicker datePicker = view.findViewById(R.id.date_picker);
            //设置日期简略显示 否则详细显示 包括:星期\周
            datePicker.setCalendarViewShown(false);
            try {
                LinearLayout ll = (LinearLayout) datePicker.getChildAt(0);
                LinearLayout ll2 = (LinearLayout) ll.getChildAt(0);
                ll2.getChildAt(2).setVisibility(View.GONE);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //设置date布局
            builder.setView(view)
                    .setTitle("选择日期")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("确定", (dialog, which) -> {
                        //日期格式
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth() + 1;
                        String date = String.format("%d-%d", year, month);
                        tv.setText(date);
                        SharedHelper.getInstance().put(key, date);
                        adapter.notifyDataSetChanged();
                        dialog.cancel();
                    })
                    .create().show();
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        SettingProfile.setProduceTimeType(adapter.getDatas().get(position));
        adapter.notifyDataSetChanged();
    }


}
